<div class="layuimini-container">
    <form id="app-form" class="layui-form layuimini-form">

        <div class="layui-form-item">
            <label class="layui-form-label required">APP名称</label>
            <div class="layui-input-block">
                <input type="text" name="app_name" class="layui-input copy-input" lay-verify="required" lay-reqtext="请输入APP名称" placeholder="请输入APP名称" value="{$row.app_name|default=''}" readonly />
                <tip>点击复制到粘贴板。</tip>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">APP KEY</label>
            <div class="layui-input-block">
                <input type="text" name="app_key" class="layui-input copy-input" lay-verify="required" lay-reqtext="请输入APP KEY" placeholder="请输入APP KEY" value="{$row.app_key|default=''}" readonly />
                <tip>点击复制到粘贴板。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">APP SECRET</label>
            <div class="layui-input-block">
                <input type="text" name="app_secret" class="layui-input copy-input" lay-verify="required" lay-reqtext="请输入APP SECRET" placeholder="请输入APP SECRET" value="{$row.app_secret|default=''}" readonly />
                <tip>点击复制到粘贴板。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">APP TOKEN</label>
            <div class="layui-input-block">
                <input type="text" name="auth_token" class="layui-input copy-input" lay-verify="required" value="{$row.auth_token|default=''}" readonly />
                <tip>点击复制到粘贴板。</tip>
            </div>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        var $ = layui.jquery,
            layer = layui.layer;;
        $(document).on('click','.copy-input',function () {
           let text = $(this).val();
            if(text === '' || text == null || text.length === 0 || /^\s+$/.test(text)) {
                return false;
            }
            if (navigator.clipboard) {
                // 使用 Clipboard API
                navigator.clipboard.writeText(text).then(function() {
                    layer.msg('文本已复制到剪贴板');
                }).catch(function(error) {
                    console.error('复制失败:', error);
                });
            } else {
                // 回退到 execCommand 方法
                const textarea = document.createElement('textarea');
                textarea.value = text;
                document.body.appendChild(textarea);
                textarea.select();
                textarea.setSelectionRange(0, 99999);
                document.execCommand('copy');
                document.body.removeChild(textarea);
                layer.msg('文本已复制到剪贴板');
            }
        });
    });

</script>
